<?php

class User_Model_Parades extends Zend_Db_Table_Abstract
{
    protected $_name = 'parades';
    protected $_primary = 'id_parada';
   
    
    private function dist($aX, $aY, $bX, $bY){
        return sqrt((($aX-$bX)*($aX-$bX))+(($aY-$bY)*($aY-$bY)));
    }
    
    public function get_10_parades_properes($posx, $posy){
        $select = $this->_db->select()
                    ->from($this->_name);
        $result = $select->query()->fetchAll();

        $res = array();
        for($i = 0; $i < sizeof($result) ; $i++){
            $d = $this->dist($posx, $posy, $result[$i]['posx'], $result[$i]['posy']);
            $res[] = array();
            $res[$i]['id'] = $result[$i]['id_parada'];
            $res[$i]['dist'] = $d;
            $res[$i]['posx'] = $result[$i]['posx'];
            $res[$i]['posy'] = $result[$i]['posy'];
        }  
        
        function cmp(array $a, array $b) {
            if ($a['dist'] == $b['dist']) {
                return 0;
            }
            return ($a['dist'] < $b['dist']) ? -1 : 1;
        }
        
        usort($res, "cmp");

        $ret = array();
        
        for($i = 0; $i < 10 && $i < sizeof($res) ; $i++){
            $ret[] = $res[$i];
        }
        return $ret;
    }
   

}
?>
